package com.yvan.spring;

import com.yvan.Conv;
import com.yvan.YvanUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户的身份识别
 * Created by luoyifan on 2017/3/21.
 */
public class IdentityVerification {

    public static final String TOKEN_KEY = "TOKEN";
    public static final String SITE_DOMAIN_KEY = "SITE_DOMAIN";
    public static final String USER_AGENT_ATTR = "USER_AGENT";

    public static HttpServletRequest currentRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    }

    public static HttpServletResponse currentResponse() {
        try {
            return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getResponse();
        } catch (Exception var1) {
            return null;
        }
    }

    public static String currentSiteDomain() {
        HttpServletRequest request = currentRequest();

        // 从 head 取
        String siteDomain = request.getHeader(SITE_DOMAIN_KEY);
        if (!StringUtils.isEmpty(siteDomain)) {
            return siteDomain;
        }

        // 从 attribute 取
        siteDomain = Conv.NS(request.getAttribute(SITE_DOMAIN_KEY));
        if (!StringUtils.isEmpty(siteDomain)) {
            return siteDomain;
        }

        // 从域名取
        //String serverName = request.getServerName();
        String hostName = currentHost();
        int portPos = hostName.indexOf(':');
        if (portPos > 0) {
            hostName = hostName.substring(0, portPos);
        }

        if (YvanUtil.isDomain(hostName)) {
            int pos = hostName.indexOf('.');
            if (pos < 0) {
                return "";
            }

            if (!hostName.substring(pos + 1).contains(".")) {
                return "";
            }

            siteDomain = hostName.substring(0, pos).toLowerCase();
            if ("www".equals(siteDomain)) {
                return "";
            }
            return siteDomain;
        }
        return "";
    }

    public static String currentRemoteIp() {
        HttpServletRequest request = currentRequest();
        if (request.getHeader("x-forwarded-for") != null) {
            return request.getHeader("x-forwarded-for");
        }
        return request.getRemoteAddr();
    }

    public static String currentHost() {
        HttpServletRequest request = currentRequest();
        String host = request.getHeader("X-Forwarded-Host");
        if (!StringUtils.isEmpty(host)) {
            return host;
        }
        return request.getHeader("Host");
    }

    public static String currentUrl() {
        HttpServletRequest request = currentRequest();
        return request.getScheme() + "://" + currentHost() + request.getRequestURI();
    }

    public static String currentToken() {
        HttpServletRequest request = currentRequest();

        String token;
        // 从head取
        token = request.getHeader(TOKEN_KEY);
        if (!StringUtils.isEmpty(token)) {
            return token;
        }

        // 从attribute取
        token = Conv.NS(request.getAttribute(TOKEN_KEY));
        if (!StringUtils.isEmpty(token)) {
            return token;
        }

        // 从cookie取
        token = YvanUtil.getCookieValue(TOKEN_KEY);
        if (!StringUtils.isEmpty(token)) {
            return token;
        }

        return null;
    }
}
